package com.zcbl.toolkit.logger;

import com.zcbl.toolkit.http.request.RequestMetaData;
import com.zcbl.toolkit.http.util.IPAddressUtils;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author he peng
 * @create 2018/5/7 14:46
 * @see
 */
public class HttpRequestSurveyBizLogFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        RequestMetaData requestMetaData = RequestMetaData.builder()
                .remoteIp(IPAddressUtils.getRequestIP(request))
                .requestUrl(request.getRequestURI())
                .build();
        SurveyBizDBLogger.setRequestMetaData(requestMetaData);
        filterChain.doFilter(servletRequest , servletResponse);
    }

    @Override
    public void destroy() {

    }
}
